package com.travel.manager.controller;


import com.github.pagehelper.PageInfo;
import com.travel.manager.service.CityService;
import common.entity.vo.SimpleResponseVO;
import common.pojo.City;
import common.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

import static common.constant.Constants.SUCCESS;

/**
 * @author liaoyangyang
 * @描述: 城市列表接口
 */
@Controller
@RequestMapping("/city")
public class CityController {

    @Autowired
    private CityService cityService;

    /**
     * 城市列表页，页面使用easyUI的datagrid
     *
     * @param city 查询条件
     * @param page 页码，参数名不能变
     * @param rows 每页几条，参数名不能变
     * @return
     */
    @RequestMapping("/list.html")
    @ResponseBody
    public Map<String, Object> list(City city, Integer page, Integer rows) {
        SimpleResponseVO responseVO = new SimpleResponseVO();
        Map<String, Object> result = new HashMap<>();
        try {
            PageInfo<City> pageInfo = cityService.getCityList(city, page, rows);
            //rows是datagrid需要的数据列表，这个key不能变
            result.put("rows", pageInfo.getList());
            //total是datagrid需要的数据总条数，这个key不能变
            result.put("total", pageInfo.getTotal());
            responseVO.setStatus(SUCCESS);
            responseVO.setResult(result);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return result;
    }

    @RequestMapping("/edit.html")
    @ResponseBody
    public Map<String, Object> addCity(City city) {
        SimpleResponseVO responseVO = new SimpleResponseVO();
        //如果没有id代表新增
        try {
            if (city.getId() == null) {
                cityService.addCity(city);
            } else {
                cityService.updateCity(city);
            }
            responseVO.setStatus(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonUtil.getOkMsg("编辑成功", null);
    }

}
